package com.view.resourcemanage.CustomerFrame;

import com.bean.Customer;
import com.lovo.netCRM.component.LovoButton;
import com.lovo.netCRM.component.LovoTxt;
import com.service.CustomerService;
import com.service.impl.CustomerServiceImpl;
import com.view.MainFrame;

import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class CustomerUpdateDialog extends JDialog {

    private LovoButton addBtn;
    private LovoButton cancelBtn;
    private JLabel nameTxt,emailTxt,addressTxt;
    private LovoTxt directorTxt,telTxt,rangeTxt;


    private MainFrame frame ;
    private int key;
    private CustomerService customerService = new CustomerServiceImpl();

    public CustomerUpdateDialog(MainFrame frame,int key) {
        // TODO Auto-generated constructor stub
        super(frame,"添加新客户",true);
        this.frame = frame;
        this.key = key;
        this.setBounds(350, 250, 520, 400);
        this.setLayout(null);
        this.init();
        this.setVisible(true);

    }



    private void init() {
        // TODO Auto-generated method stub
        Customer customer = new Customer();
        customer = customerService.getCustomerInfo(key);

        System.out.println(customer.getName());

        nameTxt = new JLabel();
        nameTxt.setBounds(20,20,100,60);
        nameTxt.setText("<html>客户："+customer.getName()+" </html>");
        this.add(nameTxt);

        emailTxt = new JLabel();
        emailTxt.setBounds(20,90,100,60);
        emailTxt.setText("<html>邮箱："+customer.getDirectorEmail()+" </html>");
        this.add(emailTxt);

        addressTxt = new JLabel();
        addressTxt.setBounds(20,160,100,60);
        addressTxt.setText("<html>地址："+customer.getAddress()+" </html>");
        this.add(addressTxt);


        telTxt = new LovoTxt("联系方式",200,30,this);


        directorTxt = new LovoTxt("负责人",200,110,this);



        rangeTxt = new LovoTxt("经营范围",200,180,this);


        this.addBtn = new LovoButton("修改", 120, 320, this);
        this.cancelBtn = new LovoButton("取消", 270, 320, this);

        cancelBtn.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                CustomerUpdateDialog.this.dispose();;
            }
        });

        addBtn.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                String directorTxtText = directorTxt.getText();
                String telTxtText = telTxt.getText();
                String rangeTxtText = rangeTxt.getText();

                if(customerService.updateCustomer(key,telTxtText,directorTxtText,rangeTxtText)){
                    JOptionPane.showMessageDialog(null,"修改成功");
                    CustomerUpdateDialog.this.dispose();
                    frame.flush();
                }else {
                    JOptionPane.showMessageDialog(null,"修改失败");
                }
            }
        });

    }


}
